package com.ruoyi.common.utils;

//经纬度距离计算
public class PositioningUtils {
    // 地球赤道半径
    private static double EARTH_RADIUS = 6371393;
    /**
     * f:from
     * t:to
     * @param fLat
     * @param fLng
     * @param tLat
     * @param tLng
     * @return
     */
    public static double getDistance(double fLat, double fLng, double tLat,
                                     double tLng) {
        double radiansAX = Math.toRadians(fLng); // A经弧度
        double radiansAY = Math.toRadians(fLat); // A纬弧度
        double radiansBX = Math.toRadians(tLng); // B经弧度
        double radiansBY = Math.toRadians(tLat); // B纬弧度

        // 公式中“cosβ1cosβ2cos（α1-α2）+sinβ1sinβ2”的部分，得到∠AOB的cos值
        double cos = Math.cos(radiansAY) * Math.cos(radiansBY) * Math.cos(radiansAX - radiansBX)
                + Math.sin(radiansAY) * Math.sin(radiansBY);
//        System.out.println("cos = " + cos); // 值域[-1,1]
        double acos = Math.acos(cos); // 反余弦值
//        System.out.println("acos = " + acos); // 值域[0,π]
//        System.out.println("∠AOB = " + Math.toDegrees(acos)); // 球心角 值域[0,180]
        double d = EARTH_RADIUS * acos;
//        BigDecimal res = new BigDecimal(d).setScale(2, RoundingMode.UP);
        //换算为公里数
        return Math.round( d / 100d) / 10d ;

    }

    public static void main(String[] args) {
        System.out.println(getDistance(39.85856,116.28616,39.857425, 116.283365));
    }
}
